package sg.edu.nus.iss.billing.parse;

import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * @author sammil
 *
 */
public class TransactionHeaderParser extends Parser {
	
	private static final int MAX_LENGTH = 2 * Parser.MAX_LINE_LEN;
	
	public static final String TRANS_HEADER_LINE = "NUMBER";
	public static final String END_LINE = "----------";
	
	private TransactionHeaderParser() {
	}

	public static final TransactionHeaderParser SINGLETON = new TransactionHeaderParser();
	
	@Override
	public <L> boolean parse(LineNumberReader in, HashMap<String,ArrayList<L>> outputMap)
			throws IOException {

		try {

			// remember where we started from
			in.mark(MAX_LENGTH);
			if (!consume(in, TRANS_HEADER_LINE)) {
				return false;
			}
			
			if (!consume(in, END_LINE)) {
				return false;
			}
		} catch (IOException e) {

			System.err.println("TransactionHeaderParser:"+ e);
			// go back to where we started
			in.reset();
			// re-throw the exception
			throw e;
		}
		System.out.println("Parsed Transaction Header ");
		return true;
		
	}

}
